GtkDialog: Update suggested-action more carefully
authorMatthias Clasen <mclasen@redhat.com>
Thu, 24 Apr 2014 05:32:50 +0000 (01:32 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Sat, 26 Apr 2014 02:11:53 +0000 (22:11 -0400)
We were not really handling all cases correctly here. We want
the suggested-action style class to only be set on headerbar
buttons, and it should be set on the default widget. Ensure
this by syncing the suggested-action style class with the
default style class. As a side-effect, setting has-default
on an action widget in ui files will now have the expected
effect.

https://bugzilla.gnome.org/show_bug.cgi?id=728846

gtk/gtkdialog.c

index b8585a137bcd68dc90b999ffe6e5e7bcd6c3c718..73ae618322272c88a99e965cf94991f63569150c 100644 (file)
@@ -440,6 +440,30 @@ add_to_action_area (GtkDialog *dialog,
     gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (priv->action_area), child, TRUE);
 }
 
+static void
+update_suggested_action (GtkDialog *dialog)
+{
+  GtkDialogPrivate *priv = dialog->priv;
+
+  if (priv->use_header_bar)
+    {
+      GList *children, *l;
+
+      children = gtk_container_get_children (GTK_CONTAINER (priv->headerbar));
+      for (l = children; l != NULL; l = l->next)
+        {
+          GtkWidget *child = l->data;
+         GtkStyleContext *context = gtk_widget_get_style_context (child);
+
+          if (gtk_style_context_has_class (context, GTK_STYLE_CLASS_DEFAULT))
+            gtk_style_context_add_class (context, GTK_STYLE_CLASS_SUGGESTED_ACTION);
+          else
+            gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SUGGESTED_ACTION);
+        }
+      g_list_free (children);
+    }
+}
+
 static void
 add_action_widgets (GtkDialog *dialog)
 {
@@ -467,12 +491,11 @@ add_action_widgets (GtkDialog *dialog)
           g_object_unref (child);
 
           if (has_default)
-            {
-              gtk_widget_grab_default (child);
-              gtk_style_context_add_class (gtk_widget_get_style_context (child), GTK_STYLE_CLASS_SUGGESTED_ACTION);
-            }
+            gtk_widget_grab_default (child);
         }
       g_list_free (children);
+
+      update_suggested_action (dialog);
     }
 }
 static GObject *
@@ -1017,7 +1040,15 @@ gtk_dialog_add_action_widget (GtkDialog *dialog,
   add_response_data (dialog, child, response_id);
 
   if (priv->constructed && priv->use_header_bar)
-    add_to_header_bar (dialog, child, response_id);
+    {
+      add_to_header_bar (dialog, child, response_id);
+
+      if (gtk_widget_has_default (child))
+        {
+          gtk_widget_grab_default (child);
+          update_suggested_action (dialog);
+        }
+    }
   else
     add_to_action_area (dialog, child, response_id);
 }
@@ -1188,17 +1219,15 @@ gtk_dialog_set_default_response (GtkDialog *dialog,
       ResponseData *rd = get_response_data (widget, FALSE);
 
       if (rd && rd->response_id == response_id)
-        {
-         gtk_widget_grab_default (widget);
-          gtk_style_context_add_class (gtk_widget_get_style_context (widget), GTK_STYLE_CLASS_SUGGESTED_ACTION);
-        }
-      else
-        gtk_style_context_remove_class (gtk_widget_get_style_context (widget), GTK_STYLE_CLASS_SUGGESTED_ACTION);
+       gtk_widget_grab_default (widget);
 
       tmp_list = g_list_next (tmp_list);
     }
 
   g_list_free (children);
+
+  if (dialog->priv->use_header_bar)
+    update_suggested_action (dialog);
 }
 
 /**